from rest_framework import serializers
from goods.models import *


class BrandSer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = '__all__'
        read_only_fields = ['id',]

# 频道组序列化器
class GroupSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.filter(cid=None))  # 所有一级类别
    # 获取当前类别的名称 只能序列化输出 不能反序列化输入  read_only=True 只读
    cate_name = serializers.SerializerMethodField(read_only=True)
    # obj:序列化的对象
    def get_cate_name(self,obj):
        return obj.cate.name
    class Meta:
        model = Group
        fields = '__all__'
        read_only_fields = ['id']

# 类别
class CateSer(serializers.ModelSerializer):
    class Meta:
        model = Cate
        fields = '__all__'
        read_only_fields = ['id',]

# SKU
class SKUSer(serializers.ModelSerializer):
    cate_name = serializers.SerializerMethodField(read_only=True)
    def get_cate_name(self,obj):
        return obj.cate.name
    class Meta:
        model = SKU
        fields = '__all__'
        # 只用于序列化输出
        read_only_fields = ['id',]

# spu
class SPUSer(serializers.ModelSerializer):
    brand_name = serializers.SerializerMethodField(read_only=True)
    def get_brand_name(self,obj):
        return obj.brand.name
    class Meta:
        model = SPU
        fields = '__all__'
        read_only_fields = ['id',]

# 商品规格
class SPUSpecSer(serializers.ModelSerializer):
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    spu_name = serializers.SerializerMethodField(read_only=True)
    def get_spu_name(self,obj):
        return obj.spu.name
    class Meta:
        model = SPUSpecification
        fields = "__all__"
        read_only_fields = ["id", ]

# 规格选项值的展示
class ValueSer(serializers.ModelSerializer):
    # spec = serializers.PrimaryKeyRelatedField(queryset=SPUSpecification.objects.all())
    spec_name = serializers.SerializerMethodField(read_only=True)
    def get_spec_name(self,obj):
        return obj.spec.name
    class Meta:
        model = SpecValue
        fields = '__all__'
        read_only_fields = ['id',]

# 商品图片
class SKUImageSer(serializers.ModelSerializer):
    sku_name = serializers.SerializerMethodField(read_only=True)
    def get_sku_name(self,obj):
        return obj.sku.name
    class Meta:
        model = SKUImg
        fields = '__all__'
        read_only_fields = ['id',]

# 
















